package com.zrspring.libv2.network

import com.zrspring.libv2.util.LogPrinter
import com.zrspring.libv2.view.ProgressDlg
import io.reactivex.Observable
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import okhttp3.ResponseBody


/**
 * Created by shopping on 2017/12/22 10:46.
 * https://github.com/wheroj
 */
object RxUtils {


    abstract class DefaultListener(private val dialog: ProgressDlg? = null) : HttpListener {

        override fun onError(e: Throwable) {
            dialog?.dismiss()
        }

        override fun onComplete() {
            dialog?.dismiss()
        }

        override fun onStart(d: Disposable) {
            dialog?.show()
        }
    }

    interface HttpListener {
        fun onError(e: Throwable)
        fun onComplete()
        fun onStart(d: Disposable)
        fun onResult(result: String)
    }

    /**
     *  利用 rxjava 访问接口数据
     * @param observable 必传，rxjava Observable<? extends BaseModel>
     * @param httpListener 必传，可传默认实现 ${link RxUtils.DefaultListener}
     */
    fun <M : BaseModel<*>, O : Observable<M>> acquire(
            observable: O, httpListener: HttpListener?) {
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : Observer<M> {
                    override fun onError(e: Throwable) {
                        httpListener?.onError(e)
                    }

                    override fun onComplete() {
                        httpListener?.onComplete()
                    }

                    override fun onNext(model: M) {
//                        when(model.code) {
//                            "1000" -> onNextListener.onNext(model)
//                            else -> {
//                                //处理其他不成功的情況
//                            }
//                        }
//                        httpListener?.onResult(model)
                    }

                    override fun onSubscribe(d: Disposable) {
                        httpListener?.onStart(d)
                    }
                })
    }

    fun acquireString(observable: Observable<ResponseBody>
            , httpListener: HttpListener? = null) {
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map { t: ResponseBody -> t.string() }
                .subscribe(object : Observer<String> {
                    override fun onError(e: Throwable) {
                        httpListener?.onError(e)
                    }

                    override fun onComplete() {
                        httpListener?.onComplete()
                    }

                    override fun onNext(model: String) {
//                        when(model.code) {
//                            "1000" -> onNextListener.onNext(model)
//                            else -> {
//                                //处理其他不成功的情況
//                            }
//                        }
                        httpListener?.onResult(model)
                    }

                    override fun onSubscribe(d: Disposable) {
                        httpListener?.onStart(d)
                    }
                })
    }

}